/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} low
 * @param {number} high
 * @return {number}
 */
var rangeSumBST = function(root, low, high) {
    let ans = []
    let res = 0
    const middleTraverse = function(node) {
        if(node.left == null && node.right == null) return node.val
        if(node.left) {
            let leftVal = middleTraverse(node.left)
            ans.push(leftVal)
        }
        ans.push(node.val)
        if(node.right) {
            let rightVal = middleTraverse(node.right)
            ans.push(rightVal)
        }
    }
    middleTraverse(root)
    ans.forEach(item => {
        if(item >= low && item <= high) res += item
    })
    return res
};